package com.ruoyi.merchantdata.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.db.domain.MerchantDataAllocateInfo;
import com.ruoyi.db.domain.MerchantUserInfo;
import com.ruoyi.db.mapper.MerchantDataAllocateInfoMapper;
import com.ruoyi.db.mapper.MerchantDataPushInfoMapper;
import com.ruoyi.db.mapper.MerchantUserInfoMapper;
import com.ruoyi.db.mapper.MemberSourceInfoMapper;
import com.ruoyi.db.domain.MerchantDataPoolInfo;
import com.ruoyi.db.mapper.MerchantDataPoolInfoMapper;
import com.ruoyi.merchantdata.service.IMerchantDataPoolInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * 商户客户公海信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-24
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class MerchantDataPoolInfoServiceImpl implements IMerchantDataPoolInfoService {
    @Autowired
    private MerchantDataPoolInfoMapper merchantDataPoolInfoMapper;

    @Autowired
    private MerchantDataAllocateInfoMapper merchantDataAllocateInfoMapper;

    @Autowired
    private MerchantUserInfoMapper merchantUserInfoMapper;

    @Autowired
    private MemberSourceInfoMapper memberSourceInfoMapper;

    @Autowired
    private MerchantDataPushInfoMapper merchantDataPushInfoMapper;


    /**
     * 查询商户客户公海信息
     *
     * @param id 商户客户公海信息主键
     * @return 商户客户公海信息
     */
    @Override
    public MerchantDataPoolInfo selectMerchantDataPoolInfoById(Long id) {
        return merchantDataPoolInfoMapper.selectMerchantDataPoolInfoById(id);
    }


    @Override
    public List<Map<String, Object>> selectMerchantDataPoolInfo(MerchantDataPoolInfo merchantDataPoolInfo) {
        if (ObjectUtil.isEmpty(merchantDataPoolInfo.getInfoId()) && !SecurityUtils.loginUserIsAdmin()) {
            merchantDataPoolInfo.setDeptId(SecurityUtils.getDeptId());
        }
        return merchantDataPoolInfoMapper.selectMerchantDataPoolInfo(merchantDataPoolInfo);
    }


    /**
     * 修改商户客户公海信息
     *
     * @param merchantDataPoolInfo 商户客户公海信息
     * @return 结果
     */
    @Override
    public int updateMerchantDataPoolInfo(MerchantDataPoolInfo merchantDataPoolInfo) {
        //修改客户跟踪逻辑
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(new Date());
        if (merchantDataPoolInfo.getRemarkInfo() != null) {
            MerchantDataPoolInfo DataPoolInfo = merchantDataPoolInfoMapper.selectMerchantDataPoolInfoById(merchantDataPoolInfo.getId());
            MerchantUserInfo merchantUserInfo = merchantUserInfoMapper.selectMerchantUserInfoById(SecurityUtils.getUserId());
            String nickName = merchantUserInfo.getNickName();
            if (DataPoolInfo.getRemarkInfo() != null) {
                String updatedRemarkInfo = DataPoolInfo.getRemarkInfo() + "&nbsp;&nbsp;&nbsp;" + formattedDate + "&nbsp;&nbsp;&nbsp;" + nickName + " : " + merchantDataPoolInfo.getRemarkInfo() + "<br/>";
                merchantDataPoolInfo.setRemarkInfo(updatedRemarkInfo);
            } else {
                String updatedRemarkInfo = formattedDate + "&nbsp;&nbsp;&nbsp;" + nickName + " : " + merchantDataPoolInfo.getRemarkInfo() + "<br/>";
                merchantDataPoolInfo.setRemarkInfo(updatedRemarkInfo);
            }
        }
        merchantDataPoolInfo.setUpdateTime(DateUtils.getNowDate());
        return merchantDataPoolInfoMapper.updateMerchantDataPoolInfo(merchantDataPoolInfo);
    }

    /**
     * 用户捞取逻辑
     *
     * @param merchantDataPoolInfo
     * @return
     */
    @Override
    public int laoquMerchantDataPoolInfo(MerchantDataPoolInfo merchantDataPoolInfo) {

        Long userId = SecurityUtils.getUserId();

        //将公海状态修改
        MerchantDataPoolInfo DataPoolInfo = merchantDataPoolInfoMapper.selectMerchantDataPoolInfoById(merchantDataPoolInfo.getId());
        merchantDataPoolInfo.setUpdateTime(DateUtils.getNowDate());
        merchantDataPoolInfo.setUserId(userId);
        int i = merchantDataPoolInfoMapper.updateMerchantDataPoolInfo(merchantDataPoolInfo);


        //添加到我的数据
        MerchantDataAllocateInfo merchantDataAllocateInfo = new MerchantDataAllocateInfo();
        merchantDataAllocateInfo.setUid(DataPoolInfo.getUid());
        merchantDataAllocateInfo.setType(2l);
        merchantDataAllocateInfo.setUserId(userId);
        merchantDataAllocateInfo.setDataId(DataPoolInfo.getId());
        merchantDataAllocateInfo.setDateInfo(DataPoolInfo.getDateInfo());
        merchantDataAllocateInfo.setInfoId(DataPoolInfo.getInfoId());
        merchantDataAllocateInfo.setDeptId(DataPoolInfo.getDeptId());
        merchantDataAllocateInfo.setStatus(0l);
        merchantDataAllocateInfo.setCreateTime(DateUtils.getNowDate());
        int i1 = merchantDataAllocateInfoMapper.insertMerchantDataAllocateInfo(merchantDataAllocateInfo);


        return i1;
    }

    @Override
    public int fenpeiMerchantDataPoolInfo(MerchantDataPoolInfo merchantDataPoolInfo) {
        //将公海状态修改
        MerchantDataPoolInfo DataPoolInfo = merchantDataPoolInfoMapper.selectMerchantDataPoolInfoById(merchantDataPoolInfo.getId());
        merchantDataPoolInfo.setStatus("1");
        merchantDataPoolInfo.setUpdateTime(DateUtils.getNowDate());
        int i = merchantDataPoolInfoMapper.updateMerchantDataPoolInfo(merchantDataPoolInfo);


        //添加到我的数据
        MerchantDataAllocateInfo merchantDataAllocateInfo = new MerchantDataAllocateInfo();
        merchantDataAllocateInfo.setUid(DataPoolInfo.getUid());
        merchantDataAllocateInfo.setType(1l);

        merchantDataAllocateInfo.setUserId(merchantDataPoolInfo.getUserId());
        merchantDataAllocateInfo.setDataId(DataPoolInfo.getId());
        merchantDataAllocateInfo.setDateInfo(DataPoolInfo.getDateInfo());
        merchantDataAllocateInfo.setInfoId(DataPoolInfo.getInfoId());
        merchantDataAllocateInfo.setDeptId(DataPoolInfo.getDeptId());
        merchantDataAllocateInfo.setStatus(0l);
        merchantDataAllocateInfo.setCreateTime(DateUtils.getNowDate());

        return merchantDataAllocateInfoMapper.insertMerchantDataAllocateInfo(merchantDataAllocateInfo);
    }

    @Override
    public List<Map<String, Object>> selectCrmData1(String startDate, String endDate) {
        return merchantDataPoolInfoMapper.selectCrmDataInfo(startDate, endDate);
    }

    @Override
    public List<Map<String, Object>> selectCrmData(Integer type) {
        LocalDate dateInfo = LocalDate.now();
        LocalDate startDate = null;
        LocalDate endDate = null;

        if (type == 0 || type == 1) {
            int date = (type == 0) ? 0 : 1;
            startDate = LocalDate.now().minusDays(date);
            endDate = LocalDate.now().minusDays(date);
        } else if (type == 2 || type == 3 || type == 4) {
            int date = (type == 2) ? 3 : (type == 4) ? 30 : 5;
            startDate = dateInfo.minusDays(date);
            endDate = LocalDate.now().minusDays(1);
        }
        // 根据条件查询 全国的数据
//        Long number = memberSourceInfoMapper.countAllumber(startDate.toString(), endDate.toString());
        List<Map<String, Object>> CrmData = merchantDataPoolInfoMapper.selectAllCrmData(startDate.toString(), endDate.toString());

//        Map<String, Object> totalMap = new HashMap<>();
//        totalMap.put("name", "总表单");
//        totalMap.put("number", number);
//        CrmData.add(0, totalMap);

        Long crmCount = merchantDataPoolInfoMapper.selectAllCrmCount(startDate.toString(), endDate.toString());
        Long ApiCOUNT = merchantDataPushInfoMapper.selectAllApiCount(startDate.toString(), endDate.toString());
        Long totalCount = crmCount + ApiCOUNT;

        Map<String, Object> totalMap1 = new HashMap<>();
        totalMap1.put("name", "总消耗");
        totalMap1.put("number", totalCount);
        CrmData.add(0, totalMap1);

//        Long number1 = merchantDataPushInfoMapper.selectAllApiBcount(startDate.toString(), endDate.toString());

//        Map<String, Object> totalMap2 = new HashMap<>();
//        totalMap2.put("name", "双授权消耗");
//        totalMap2.put("number", number1);
//        CrmData.add(2, totalMap2);
        return CrmData;

    }

}
